package com.tuanzi.loan.business.repository.system.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanMenu.LOAN_MENU;

import java.util.List;
import java.util.stream.Collectors;

import org.jooq.impl.TableImpl;
import org.springframework.beans.factory.annotation.Autowired;

import com.tuanzi.loan.business.entity.system.FunctionalPermission;
import com.tuanzi.loan.business.entity.system.Menu;
import com.tuanzi.loan.business.repository.system.FunctionalPermissionRepository;
import com.tuanzi.loan.business.repository.system.custom.MenuRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;

/**
 * @author Ray 2017/09/21
 */
@SuppressWarnings({ "rawtypes" })
public class MenuRepositoryImpl extends JOOQBaseRepository implements MenuRepositoryCustom {

	@Autowired
	private FunctionalPermissionRepository functionalPermissionRepository;

	@Override
	protected TableImpl getTable() {
		return LOAN_MENU;
	}

	@Override
	public List<Menu> findAllByUserId(String userId) {
		List<String> permissionCodes = functionalPermissionRepository.findAllPermissionByUserId(userId).parallelStream().map(FunctionalPermission::getCode)
				.collect(Collectors.toList());

		in(LOAN_MENU.CODE, permissionCodes);
		eq(LOAN_MENU.ENABLE, true);

		return search(Menu.class);
	}
}
